<template>
  <ele-page>
    <!-- 搜索表单 -->
    <place-search @search="reload"/>
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        cache-key="systemPlaceTable"
      >
        <template #toolbar>
          <el-button
            type="primary"
            class="ele-btn-icon"
            :icon="Plus"
            @click="openEdit()"
          >
            新建
          </el-button>
          <el-button class="ele-btn-icon" :icon="Upload" @click="openImport">
            导入
          </el-button>
          <el-button class="ele-btn-icon" :icon="Download" @click="exportData">
            导出
          </el-button>
        </template>
        <template #action="{ row }">
          <el-link type="primary" :underline="false" @click="openEdit(row)">
            修改
          </el-link>
          <el-divider direction="vertical"/>
          <el-link type="success" :underline="false" @click="fillPlaceFullName(row)">
            填充
          </el-link>
          <el-divider direction="vertical"/>
          <el-link type="danger" :underline="false" @click="removeBatch(row)">
            删除
          </el-link>
        </template>
        <template #extendType="{ row }">
          <el-tag type="" size="small" v-if="row.isNeedExtend == '0'">填充</el-tag>
          <el-tag type="danger" size="small" v-if="row.isNeedExtend == '1'">不填充</el-tag>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <place-edit v-model="showEdit" :data="current" @done="refresh"/>

    <!-- 导入弹窗 -->
    <place-import v-model="showImport" @done="reload"/>
  </ele-page>
</template>

<script setup>
import {ref} from 'vue';
import {Plus, Aim, Upload, Download} from '@element-plus/icons-vue';
import {ElMessageBox} from 'element-plus/es';
import {EleMessage} from 'ele-admin-plus/es';
import PlaceSearch from './components/place-search.vue';
import PlaceEdit from './components/place-edit.vue';
import PlaceImport from './components/place-import.vue';
import {pagePlaces, removePlaces, exportPlaces, fillFullName} from '@/api/system/place';

/** 表格实例 */
const tableRef = ref(null);

/** 表格列配置 */
const columns = ref([
  {
    type: 'selection',
    columnKey: 'selection',
    width: 50,
    align: 'center',
    fixed: 'left'
  },
  {
    prop: 'name',
    label: '地区名称',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'code',
    label: '地区代码',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'fullName',
    label: '地区全称',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'province',
    label: '省份',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'city',
    label: '市',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'county',
    label: '区县',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'isNeedExtend',
    label: '填充标识',
    align: 'center',
    minWidth: 110,
    slot: 'extendType'
  },
  {
    prop: 'remark',
    label: '备注',
    align: 'center',
    minWidth: 110
  },
  {
    columnKey: 'action',
    label: '操作',
    width: 180,
    align: 'center',
    slot: 'action'
  }
]);

/** 表格选中数据 */
const selections = ref([]);

/** 当前编辑数据 */
const current = ref(null);

/** 是否显示编辑弹窗 */
const showEdit = ref(false);

/** 是否显示导入弹窗 */
const showImport = ref(false);

/** 表格数据源 */
const datasource = ({page, limit, where}) => {
  return pagePlaces({...where, pageNum: page, pageSize: limit});
};

/** 搜索 */
const reload = (where) => {
  tableRef.value?.reload?.({page: 1, where});
};

/**
 * 刷新表格
 */
const refresh = () => {
  tableRef.value.reload();
};

/** 打开编辑弹窗 */
const openEdit = (row) => {
  current.value = row ?? null;
  showEdit.value = true;
};

/** 批量删除 */
const removeBatch = (row) => {
  const rows = row == null ? selections.value : [row];
  if (!rows.length) {
    EleMessage.error('请至少选择一条数据');
    return;
  }
  ElMessageBox.confirm(
    `是否确认删除地区名称为"${rows.map((d) => d.name).join()}"的数据项?`,
    '系统提示',
    {type: 'warning', draggable: true}
  )
    .then(() => {
      const loading = EleMessage.loading('请求中..');
      removePlaces(rows.map((d) => d.id))
        .then(() => {
          loading.close();
          EleMessage.success('删除成功');
          reload();
        })
        .catch((e) => {
          loading.close();
          EleMessage.error(e.message);
        });
    })
    .catch(() => {
    });
};

/** 导出数据 */
const exportData = () => {
  const loading = EleMessage.loading('请求中..');
  tableRef.value?.fetch?.(({where}) => {
    exportPlaces(where)
      .then(() => {
        loading.close();
      })
      .catch((e) => {
        loading.close();
        EleMessage.error(e.message);
      });
  });
};


/** 打开导入弹窗 */
const openImport = () => {
  showImport.value = true;
};


const fillPlaceFullName = (row) => {
  const loading = EleMessage.loading('请求中..');
  fillFullName(row.id).then(() => {
    loading.close();
    EleMessage.success('操作成功');
    // reload();
    tableRef.value.reload();//刷新表格
  })
    .catch((e) => {
      loading.close();
      EleMessage.error(e.message);
    });
  ;
}
</script>
